package com.hzqy.web.filter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.hzqy.commons.utils.sensitive.SensitiveWordFilter;
import com.hzqy.service.sensitive.SensitiveWordsServiceBean;

/**
 * 
 * 容器初始化(spring bean都加载完成后) 执行初始化操作： 1. 初始化敏感词操作
 * 
 * @author Administrator
 *
 */
@Component
public class SensitiveWordInitialization implements ApplicationListener<ContextRefreshedEvent>{

	private final static Logger LOG = LoggerFactory.getLogger(SensitiveWordInitialization.class);

	@Resource
	private SensitiveWordsServiceBean sensitiveWordsServiceBean;
	
	public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
		ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
		if(applicationContext.getParent() == null) {
			LOG.debug("SensitiveWordInitialization--开始初始化敏感词操作.");
			Map<String,Object> map = new HashMap<String, Object>();
			//map.put("f_ssw_status", 1);
			
			List<String> sesitiveWordList = sensitiveWordsServiceBean.selectAllSevsitiveWords(map);
			
			//初始化一个对象并注入静态变量 注入方法只能注入一次，使用SensitiveWordFilter.getInstance();方法获取对象
			SensitiveWordFilter filter = new SensitiveWordFilter(sesitiveWordList);
			SensitiveWordFilter.setInstance(filter);
			
			LOG.debug("SensitiveWordInitialization--开始初始化敏感词操作完成 本次获取敏感词: " + sesitiveWordList.size() + ".");
		}
		
	}

}
